package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.dto.system.AssginMenuDto;
import com.atguigu.spzx.model.entity.system.SysMenu;
import com.atguigu.spzx.model.entity.system.SysRoleMenu;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.spzx.manager.service.SysRoleMenuService;
import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Windows
 * @description 针对表【sys_role_menu(角色菜单)】的数据库操作Service实现
 * @createDate 2024-01-29 15:43:04
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
        implements SysRoleMenuService {

    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {

        // 所有菜单信息
        List<SysMenu> menuList = sysMenuService.findNodes();

        // 获取角色的菜单id
        List<Long> roleIdList = this.list(new LambdaQueryWrapper<SysRoleMenu>()
                        .eq(SysRoleMenu::getRoleId, roleId)
                        .eq(SysRoleMenu::getIsHalf, 0))
                .stream().map(SysRoleMenu::getMenuId)
                .collect(Collectors.toList());

        // 将数据存储到Map中进行返回
        Map<String, Object> result = new HashMap<>();
        result.put("sysMenuList", menuList);
        result.put("roleMenuIds", roleIdList);

        return result;
    }

    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        // 先删除，再添加
        this.remove(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, assginMenuDto.getRoleId()));

        List<SysRoleMenu> sysRoleMenus = assginMenuDto.getMenuIdList().stream()// 将每个Map的条目转换为流,以供操作每个map的键值对
                .map(entry -> {
                    SysRoleMenu sysRoleMenu = new SysRoleMenu();
                    // 安全类型转换，避免ClassCastException
                    Long menuId = null;
                    Integer isHalf = null;
                    if (entry.get("id") instanceof Number) {
                        menuId = ((Number) entry.get("id")).longValue(); // 安全地从Number转换为Long
                        sysRoleMenu.setMenuId(menuId);
                    }
                    if (entry.get("isHalf") instanceof Number) {
                        isHalf = ((Number) entry.get("isHalf")).intValue(); // 安全地从Number转换为Integer
                        sysRoleMenu.setIsHalf(isHalf);
                    }
                    sysRoleMenu.setRoleId(assginMenuDto.getRoleId());
                    return sysRoleMenu;
                })
                .collect(Collectors.toList());

        this.saveBatch(sysRoleMenus);
    }
}




